//
//  HistoryViewController.m
//  网易公开课项目
//
//  Created by 林 艺 on 13-11-20.
//  Copyright (c) 2013年 Marklin. All rights reserved.
//

#import "HistoryViewController.h"
#import "CourseCell.h"
#import "CourseModel.h"
#import "UIImageView+WebCache.h"  
#import "DetailViewController.h"
#import "LoadCourseDataManager.h"

@interface HistoryViewController ()

@end

@implementation HistoryViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"播放记录";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self _initNavigationBarItem];
    [self.tableView registerNib:[UINib nibWithNibName:@"CourseCell" bundle:nil] forCellReuseIdentifier:@"CourseCell"];
    [self readHistoryData];
    
}

#pragma mark --
#pragma 归档

- (void)readHistoryData
{
    LoadCourseDataManager *dataManager = [LoadCourseDataManager shareInstance];
    NSMutableArray *dataModels = [NSMutableArray arrayWithArray:[dataManager loadHistoryData]];
    if (_historyArray == nil)
    {
        _historyArray = [[NSMutableArray alloc] init];
    }
    _historyArray = dataModels;
    if (_historyArray.count == 0)
    {
        self.noRecordView.hidden = NO;
    }
    else
    {
        self.noRecordView.hidden = YES;
    }
    [self.tableView reloadData];
}

- (void)_initNavigationBarItem
{
    //返回
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    [back setBackgroundImage:[UIImage imageNamed:@"btn_left.png"] forState:UIControlStateNormal];
    
    [back setTitle:@"返回" forState:UIControlStateNormal];
    back.titleLabel.font = [UIFont systemFontOfSize:11];
    [back addTarget:self action:@selector(backClicked) forControlEvents:UIControlEventTouchUpInside];
    back.frame = CGRectMake(0, 0, 50, 34);
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:back];
    self.navigationItem.leftBarButtonItem = leftButton;
    
    //编辑按钮
    _editButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_editButton setBackgroundImage:[UIImage imageNamed:@"btn_right.png"] forState:UIControlStateNormal];
    
    [_editButton setTitle:@"编辑" forState:UIControlStateNormal];
    
    _editButton.titleLabel.font = [UIFont systemFontOfSize:11];
    [_editButton addTarget:self action:@selector(editButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    _editButton.frame = CGRectMake(0, 0, 50, 34);
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:_editButton];
    self.navigationItem.rightBarButtonItem = rightButton;
}
- (void)editButtonClicked:(UIButton *)button;
{
    BOOL flag = !self.tableView.editing;
    if (flag)
    {
        [button setTitle:@"完成" forState:UIControlStateNormal];
    }
    else
    {
        [button setTitle:@"编辑" forState:UIControlStateNormal];
    }
    [UIView animateWithDuration:0.3 animations:^{
        self.tableView.editing = flag;
    }];
}


- (void)backClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _historyArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CourseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CourseCell"];
    CourseModel *model = [_historyArray objectAtIndex:indexPath.row];
    cell.titleLabel.text = model.title;
    cell.classLabel.text = @"";
    cell.classMarkLabel.frame = CGRectMake(cell.classMarkLabel.frame.origin.x, cell.classMarkLabel.frame.origin.y, 200, cell.classMarkLabel.frame.size.height);
    if (model.lastPlayPnumber == nil)
    {
        cell.classMarkLabel.text = @"未观看";
    }
    else
    {
        if (model.lastPlaySeconds < 60)
        {
            cell.classMarkLabel.text = [NSString stringWithFormat:@"看到: 第%@课少于1分钟", model.lastPlayPnumber];
        }
        else
        {
            cell.classMarkLabel.text = [NSString stringWithFormat:@"看到: 第%@课 %02d:%02d", model.lastPlayPnumber, model.lastPlaySeconds/60, model.lastPlaySeconds%60];
        }
        
    }

    cell.playCountLabel.text = [NSString stringWithFormat:@"%@", model.playcount];
    cell.updatePlayCountLabel.text = [NSString stringWithFormat:@"%@", model.updatedPlaycount];
    [cell.courseImageView setImageWithURL:[NSURL URLWithString:model.imagePath]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 92;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detailController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    
    CourseModel *model = [_historyArray objectAtIndex:indexPath.row];
    detailController.videoCode = model.plid;
    
    //隐藏tabbarView;
    UIView * view =[self.tabBarController valueForKey:@"tabbarView"];
    view.frame = CGRectMake(-320, view.frame.origin.y, view.frame.size.width, view.frame.size.height);
    
    [self.navigationController pushViewController:detailController animated:YES];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    CourseModel *selectModel = [_historyArray objectAtIndex:indexPath.row];
    LoadCourseDataManager *dataManager = [LoadCourseDataManager shareInstance];
    
    NSMutableArray *dataModels = [NSMutableArray arrayWithArray:[dataManager loadHistoryData]];
    
    NSInteger index = 0;
    for (int i = 0; i < dataModels.count; i++)
    {
        CourseModel *model = dataModels[i];
        if (model.title == selectModel.title)
        {
            index = i;
            break;
        }
    }
    [dataModels removeObjectAtIndex:index];
    [dataManager writeToHistoryArchiverWithArray:dataModels];
    [self readHistoryData];
    
}

#pragma mark --
#pragma mark viewWillAppear
-(void)viewWillAppear:(BOOL)animated
{
    //设置全屏
    UIView *tabbarContentView=[self.tabBarController valueForKey:@"viewControllerTransitionView"];
    tabbarContentView.frame=CGRectMake(0, 0, 320, 480);
    [self readHistoryData];
}
@end
